local huangtian = fk.CreateSkill {

  name = "joyex__huangtian$",

  tags = {  },

}



huangtian:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.BuryVictim, fk.AfterPropertyChange},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self
    elseif event == fk.BuryVictim then
      return target:hasSkill(self, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local attached_huangtian = player.kingdom == "qun" and table.find(room.alive_players, function (p)
      return p ~= player and p:hasSkill(self, true)
    end)
    if attached_huangtian and not player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "joyex__huangtian_other&", nil, false, true)
    elseif not attached_huangtian and player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "-joyex__huangtian_other&", nil, false, true)
    end
  end,
})
huangtian:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.BuryVictim, fk.AfterPropertyChange},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self
    elseif event == fk.BuryVictim then
      return target:hasSkill(self, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local attached_huangtian = player.kingdom == "qun" and table.find(room.alive_players, function (p)
      return p ~= player and p:hasSkill(self, true)
    end)
    if attached_huangtian and not player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "joyex__huangtian_other&", nil, false, true)
    elseif not attached_huangtian and player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "-joyex__huangtian_other&", nil, false, true)
    end
  end,
})
huangtian:addEffect(fk.BuryVictim, {
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.BuryVictim, fk.AfterPropertyChange},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self
    elseif event == fk.BuryVictim then
      return target:hasSkill(self, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local attached_huangtian = player.kingdom == "qun" and table.find(room.alive_players, function (p)
      return p ~= player and p:hasSkill(self, true)
    end)
    if attached_huangtian and not player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "joyex__huangtian_other&", nil, false, true)
    elseif not attached_huangtian and player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "-joyex__huangtian_other&", nil, false, true)
    end
  end,
})
huangtian:addEffect(fk.AfterPropertyChange, {
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.BuryVictim, fk.AfterPropertyChange},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self
    elseif event == fk.BuryVictim then
      return target:hasSkill(self, true, true)
    elseif event == fk.AfterPropertyChange then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local attached_huangtian = player.kingdom == "qun" and table.find(room.alive_players, function (p)
      return p ~= player and p:hasSkill(self, true)
    end)
    if attached_huangtian and not player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "joyex__huangtian_other&", nil, false, true)
    elseif not attached_huangtian and player:hasSkill("joyex__huangtian_other&", true, true) then
      room:handleAddLoseSkills(player, "-joyex__huangtian_other&", nil, false, true)
    end
  end,
})

huangtian:addEffect(fk.CardUsing, {
  name = "#joyex__huangtian_trigger&",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(huangtian.name) and target.kingdom == "qun" and target ~= player and not data.card:isVirtual() and data.card.name == "jink" 
    and player:usedSkillTimes(huangtian.name, Player.HistoryTurn) < 1 and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,huangtian.name,nil,"#joyex__huangtian-get::"..target.id..":"..data.card:toLogString()) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if not player.dead then
      player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
    end
  end,
})
huangtian:addEffect(fk.CardResponding, {
  name = "#joyex__huangtian_trigger&",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(huangtian.name) and target.kingdom == "qun" and target ~= player and not data.card:isVirtual() and data.card.name == "jink" 
    and player:usedSkillTimes(huangtian.name, Player.HistoryTurn) < 1 and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,huangtian.name,nil,"#joyex__huangtian-get::"..target.id..":"..data.card:toLogString()) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if not player.dead then
      player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
    end
  end,
})

return huangtian